/*Copyright (C),2023 , NANOCHAP
*File name:   ENS_UART.C          
*Author:      Martin
*Version:     V1.0 
*Date:        2023-08-11                  
*Description: driver for uart0/1  
*Function List: 
   1. 
   2.
   3.
History: 
   1.V1.0
      Date:          
      Author: 
      Modification:
*/
#include "CMSDK_CM0.h"
#include "MY_HEADER.h"
#define UART0_RX   GPIO_2
#define UART0_TX   GPIO_3
#define UART0_RTS  GPIO_4
#define UART0_CTS  GPIO_5

#define UART1_RX  GPIO_12
#define UART1_TX  GPIO_13 
#define UART1_RTS  GPIO_14
#define UART1_CTS  GPIO_15 

typedef struct uart_para{
	uint32_t baud;
	
	
}UART_InitStructure;
UART_InitStructure uart_para0;
UART_InitStructure uart_para1;

//uart初始化，参数1：uart0/1，参数2：uart参数设置结构体
void UART_Init(CMSDK_UART_TypeDef *CMSDK_UART, UART_InitStructure uart_paraX){
	uint16_t divisor_value;
	uint8_t overSamp_mode;
	if(((CMSDK_UART->MDR)&0x00000001) == 0)
	{
		overSamp_mode = 16;
	}
	else
	{
		overSamp_mode = 13;
	}
	//
	
	
	divisor_value = (uint16_t)(((uint32_t)SYSTEM_FREQ / uart_paraX.baud / overSamp_mode) - 1);
	CMSDK_UART->DLL = (uint8_t)(divisor_value & 0x0011);
	CMSDK_UART->DLH = (uint8_t)((divisor_value & 0x1100)>>8);
}



unsigned char UartPutc(CMSDK_UART_TypeDef *CMSDK_UART,unsigned char my_ch)
{
  while ((CMSDK_UART->LSR & CMSDK_UART_LSR_THR_EMPTY_Msk) == 0x0); 
  CMSDK_UART->THR = my_ch; 
  return (my_ch);
}



